package main

import (
	"bytes"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"sync"
)

var bufferPool = sync.Pool{
	New: func() interface{} {
		return new(bytes.Buffer)
	},
}

func marshalLog(logData map[string]interface{}) []byte {
	//buf := bufferPool.New().(*bytes.Buffer) // New()是直接生成一个对象，貌似并没有做内存复用
	buf := bufferPool.Get().(*bytes.Buffer)
	json.NewEncoder(buf).Encode(logData)
	data := make([]byte, buf.Len())
	copy(data, buf.Bytes())
	buf.Reset()
	bufferPool.Put(buf)
	return data
}

func demo1_1(c *gin.Context) {

	data := map[string]interface{}{}
	data["key1"] = "valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue1"
	data["key2"] = "valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue2"
	data["key3"] = "valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue3"
	data["key4"] = "valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue4"

	c.Header("Content-Type", "application/json; charset=utf-8")
	c.Status(http.StatusOK)
	c.Writer.Write(marshalLog(data))
}
